package logic

import (
	"context"
	"errors"
	"novel-start/models/mysql"

	"novel-start/novelrpc/internal/svc"
	"novel-start/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BookShelfDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookShelfDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookShelfDelLogic {
	return &BookShelfDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BookShelfDelLogic) BookShelfDel(in *novelrpc.BookShelfDelRequest) (*novelrpc.BookShelfDelResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	begin := mysql.DB.Begin()

	if err := user.FindUserById(mysql.DB, in.UserId); err != nil {
		begin.Rollback()
		return nil, errors.New("用户不存在")
	}

	var bookShelf mysql.BookShelf

	if err := bookShelf.BookShelfDel(mysql.DB, in.BookShelfId); err != nil {
		begin.Rollback()
		return nil, err
	}

	begin.Commit()

	return &novelrpc.BookShelfDelResponse{}, nil
}
